package cn.wenjie.gd.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import cn.wenjie.gd.domain.Role;
import cn.wenjie.gd.domain.User;
import cn.wenjie.gd.service.UserService;
import cn.wenjie.gd.service.UserServiceImp;
import cn.wenjie.gd.util.UserPage;
import cn.wj.base.annotation.Packing;
import cn.wj.base.annotation.RequestMapping;
import cn.wj.base.controller.BaseController;
import cn.wj.base.util.AdviceImp;
import cn.wj.base.util.Page;
import cn.wj.base.util.ProxyUtil;

@WebServlet("/home")
public class LoginController extends BaseController {
	private UserService userService= ProxyUtil.getProx(new UserServiceImp(),new AdviceImp());
	
	@RequestMapping(method = "login")
	public String toLogin(HttpServletRequest request, HttpServletResponse response) {
		return "/pages/login.jsp";
	}

	@RequestMapping(method = "index")
	public String toIndex( HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		return "/pages/index.jsp";
	}
	
	@RequestMapping(method = "logindo",auto=false)
	public String loginDo(@Packing(name = "loginName", type = String.class) String loginName,
			@Packing(name = "pwd", type = String.class) String password,
			@Packing(name = "code", type = String.class) String code,
			HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
			String checkCode=(String) getSession("checkCode");
			if(code==null || checkCode==null){
				response.sendRedirect(request.getContextPath()+"/home/login.do"); 
			}else{
				if(!checkCode.equals(code)){
					addAttribute("ms","验证码错误！");
					//response.sendRedirect(request.getContextPath()+"/home/login.do");
					request.getRequestDispatcher("/pages/login.jsp").forward(request, response);
				}else {
					User user= userService.findUser(loginName,password);
					if(user!=null){
						setSession("sessionUeser", user);
						response.sendRedirect(request.getContextPath()+"/home/index.do");
					}else {
						addAttribute("ms","帐号或密码错误！");
						request.getRequestDispatcher("/pages/login.jsp").forward(request, response);
						//response.sendRedirect(request.getContextPath()+"/home/login.do");
					}
				}
			}
			
			return  null;
	}
	
	@RequestMapping(method = "doLogout",auto=false)
	public String doLogout(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		removeSession("sessionUeser");
		response.sendRedirect(request.getContextPath()+"/home/login.do"); 
		return null;
	}
	
	@RequestMapping(method="toAddUser")
	public String toAddUser(HttpServletRequest request,HttpServletResponse response){
		List<Role> roleList=userService.getAllRole();
		User loginUser=(User)getSession("sessionUeser");
		if(loginUser.getRole()!=3){
			Iterator<Role> sListIterator = roleList.iterator();  
			while(sListIterator.hasNext()){  
				Role r = sListIterator.next();  
			    if(r.getId()!=1){  
			    	sListIterator.remove();  
			    }  
			}  
		}
		
		addAttribute("roleList", roleList);
		return "/pages/user/addUser.jsp";
	}
	
	@RequestMapping(method="AddUser",auto=false)
	public String addUser(@Packing(name = "user", type = User.class) User user, HttpServletRequest request,HttpServletResponse response) throws IOException{
		user.setPassword("123");
		userService.save(user);
		response.sendRedirect(request.getContextPath()+"/home/userList.do");
		return null;
	}
	
	@RequestMapping(method="userList")
	public String userList(@Packing(name = "page", type = UserPage.class) UserPage page,HttpServletRequest request,HttpServletResponse response){
		page.setUrl(request.getRequestURI());
		
		String strpageSize= request.getParameter("pageSize");
		if(strpageSize!=null && !strpageSize.equals("")) page.setPageSize(Integer.parseInt(strpageSize));
		
		String strpageNum= request.getParameter("pageNum");
		if(strpageNum!=null && !strpageNum.equals("")) page.setPageNum(Integer.parseInt(strpageNum));
		
		UserPage userPage=userService.getPageList(page);
		addAttribute("page", userPage);
		List<Role> roleList=userService.getAllRole();
		addAttribute("roleList", roleList);
		return "/pages/user/userList.jsp";
	}
	
	@RequestMapping(method="delete",auto=false)
	public String delete(@Packing(name = "id", type = Integer.class) Integer id,HttpServletRequest request,HttpServletResponse response) throws IOException{
		userService.delete(id);
		response.sendRedirect(request.getContextPath()+"/home/userList.do");
		return null;
	}
	
	@RequestMapping(method="toUpdate")
	public String toUpdate(@Packing(name = "id", type = Integer.class) Integer id,HttpServletRequest request,HttpServletResponse response) {
		User user=userService.getById(id);
		List<Role> roleList=userService.getAllRole();
		User loginUser=(User)getSession("sessionUeser");
		if(loginUser.getId().equals(user.getId())){
			Iterator<Role> sListIterator = roleList.iterator();  
			while(sListIterator.hasNext()){  
				Role r = sListIterator.next();  
			    if(r.getId()!=user.getRole()){  
			    	sListIterator.remove();  
			    }  
			} 
		}else if(loginUser.getRole()!=3){
			Iterator<Role> sListIterator = roleList.iterator();  
			while(sListIterator.hasNext()){  
				Role r = sListIterator.next();  
			    if(r.getId()!=1){  
			    	sListIterator.remove();  
			    }  
			} 
		}
		addAttribute("user", user);
		addAttribute("roleList", roleList);
		return "/pages/user/update.jsp";
	}

	@RequestMapping(method="update",auto=false)
	public String update(@Packing(name = "user", type = User.class) User user,HttpServletRequest request,HttpServletResponse response) throws IOException {
		User user2=userService.getById(user.getId());
//		user.setPoint(user2.getPoint());
		user.setLoginName(user2.getLoginName());
		user.setPassword(user2.getPassword());
		userService.update(user);
		response.sendRedirect(request.getContextPath()+"/home/userList.do");
		return null;
	}
	
	
	@RequestMapping(method="show")
	public String show(@Packing(name = "id", type = Integer.class) Integer id,HttpServletRequest request,HttpServletResponse response) {
		User user=userService.getById(id);
		addAttribute("user", user);
		List<Role> roleList=userService.getAllRole();
		addAttribute("roleList", roleList);
		return "/pages/user/show.jsp";
	}
	
	
	@RequestMapping(method="editPassword",auto=false,isAjax=true)
	public String editPassword(@Packing(name = "password", type = String.class) String password, HttpServletRequest request,HttpServletResponse response){
		User user=(User) request.getSession().getAttribute("sessionUeser");
		user.setPassword(password);
		userService.update(user);
		return "Sucess";
	}
	
	@RequestMapping(method="checkUserName",auto=false,isAjax=true)
	public String checkUserName(@Packing(name = "login_name", type = String.class) String login_name, HttpServletRequest request,HttpServletResponse response){
		if(userService.checkUserName(login_name)) return "no";
		return "yes";
	}
	
}
